package com.loit.business.task;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * [定时任务案例]接口
 * Created by lifh on 2022-12-21.
 * 定时任务，主类加上@Component和@EnableScheduling注解，在方法上加上@Scheduled注解
 * 注意的是，可以加个开关来决定任务是否执行
 */
@Slf4j
@Component
@EnableScheduling
@Transactional(readOnly = true)
public class DemoTask {

    /**
     * 日志工具   可用@Slf4j代替
     */
//    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 从配置文件中读取开关的值
     */
    @Value("${task.switch.is-open}")
    private boolean flag;

    @Scheduled(cron = "${task.corn.task-bj-corn}")
    public void dealStart() {
        //判断开关状态
        if (flag) {
            SimpleDateFormat stf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = stf.format(new Date());
            log.info("定时任务启动->>" + format);
            log.info("massage---start->>" + format);
            dealBeijingMet();
        } else {
            log.info("未开message定时任务开关，请检查！");
        }
    }


    /**
     * 定时任务业务方法案例
     */
    @Transactional(readOnly = false)
    public void dealBeijingMet(){
        log.info("fackyou！--- fackyou！--- fackyou！--- fackyou！--- fackyou！ ");
        log.info("定时任务结束end");
    }

}
